/*
 *    Copyright 2009-2019 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.lm.base;

import com.github.pagehelper.PageHelper;
import ognl.Ognl;
import ognl.OgnlException;
import org.apache.ibatis.exceptions.ExceptionFactory;
import org.apache.ibatis.executor.ErrorContext;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import org.lm.base.mapper.UserMapper;
import org.lm.base.mapper.clazzMapper;
import org.lm.base.mapper.companyMapper;
import org.lm.base.pojo.Clazz;
import org.lm.base.pojo.Company;
import org.lm.base.pojo.Student;
import org.lm.base.pojo.User;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Auther: Min.Liu
 * @Description:
 * @Date: Created in 9:27 2025/3/26
 */
public class Demo1 {
  private SqlSession sqlSession;

  @Before
  public void before() throws IOException {
    String resource = "org/lm/base/mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    sqlSession = sqlSessionFactory.openSession(true);
  }

  @Test
  public void testselect() {
    companyMapper mapper = sqlSession.getMapper(companyMapper.class);
    List<Company> list = mapper.selectAll();
    for (Company company : list) {
      System.out.println(company);
    }
  }

  @Test
  public void testpageHelper() {
    companyMapper mapper = sqlSession.getMapper(companyMapper.class);
    PageHelper.startPage(1,2);
    List<Company> list = mapper.selectAll();
    for (Company company : list) {
      System.out.println(company);
    }
  }

  @Test
  public void localtestpageHelper() {
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    PageHelper.startPage(1,2);
    List<User> list = mapper.selectUser();
    for (User user : list) {
      System.out.println(user);
    }
  }

  @Test
  public void testselect4() {
    companyMapper mapper = sqlSession.getMapper(companyMapper.class);
    Company company = new Company();
    company.setCompanyNo("101012");
    company.setCompanyName("刘敏集团");
    company.setAddress("武汉");
    company.setBoss("刘敏");
    company.setRegisteredTime(LocalDateTime.now());
    int insert = mapper.insert(company);
    System.out.println(insert);
  }

  @Test
  public void testselect2() {
    clazzMapper mapper = sqlSession.getMapper(clazzMapper.class);
    List<Clazz> list = mapper.selectByids(new int[]{1,2});
    for (Clazz clazz : list) {
      System.out.println(clazz);
    }
  }

  @Test
  public void testselect3() {
    clazzMapper mapper = sqlSession.getMapper(clazzMapper.class);
    Clazz clazz = mapper.selectByid2(1);
    System.out.println(clazz);
  }

  @Test
  public void testMetaObject() {
    Clazz clazz = new Clazz();
    clazz.setCid(1);
    clazz.setCname("一班");
    Student student = new Student();
    student.setSid(1);
    student.setSname("刘敏");
    student.setClazz(clazz);
    MetaObject metaObject = MetaObject.forObject(student, new DefaultObjectFactory(), new DefaultObjectWrapperFactory(), new DefaultReflectorFactory());
    System.out.println(metaObject.getValue("sid"));
    System.out.println(metaObject.getValue("sname"));
    Object value = metaObject.getValue("clazz.cname");
    System.out.println(value);
  }

  @Test
  public void testErrorContext() {
    ExecutorService executorService = Executors.newFixedThreadPool(5);
    CountDownLatch latch = new CountDownLatch(10);
    for (int i = 0; i < 10; i++) {
      final int j = i;
      executorService.execute(() -> {
        ErrorContext errorContext = ErrorContext.instance();
        try {
          errorContext.activity("在第" + j + "个流程中出现异常")
            .object(this.getClass().getName())
            .sql("select * from usemr")
            .resource("user.xml");
          if (new Random().nextInt(10) > 5) {
            int m = 1 / 0;
          }
        } catch (Exception e) {
          throw ExceptionFactory.wrapException("has errors", e);
        }
        latch.countDown();
      });
    }
  }

  @Test
  public void testOgnl() {
    Clazz clazz = new Clazz();
    clazz.setCid(1);
    clazz.setCname("一班");
    Student student = new Student();
    student.setSid(1);
    student.setSname("刘敏");
    student.setClazz(clazz);
    HashMap<Object, Object> map = new HashMap<>();
    map.put("tq","qing");
    Object value = null;
    Object value2 = null;
    Object value3 = null;
    try {
      value = Ognl.getValue("sname", map, student);
      value2 = Ognl.getValue("clazz.cname", map, student);
      value3 = Ognl.getValue("#tq", map, student);
    } catch (OgnlException e) {
      e.printStackTrace();
    }
    System.out.println(value);
    System.out.println(value2);
    System.out.println(value3);

  }
}
